package us.sunrisemorning.pixivbrower.web.controller;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;

import us.sunrisemorning.pixivbrower.dao.ConfigService;
import us.sunrisemorning.pixivbrower.model.Config;

public class ConfigController extends Controller {

    @Inject
    private ConfigService configService;

    public void index() {
        setAttr("threadAmount", configService.getConfig(Config.THREAD_AMOUNT, "1"));
        setAttr("cacheSize", configService.getConfig(Config.CACHE_SIZE, "10000"));
        setAttr("useProxy", configService.getConfig(Config.USE_PROXY, ""));
        setAttr("proxyHost", configService.getConfig(Config.PROXY_HOST, ""));
        setAttr("proxyPort", configService.getConfig(Config.PROXY_PORT, ""));
        if ("true".equals(getPara("saved"))) {
            setAttr("saved", "true");
        }
        renderTemplate("config.html");
    }

    public void save() {
        configService.setConfig(Config.THREAD_AMOUNT, getPara("threadAmount"));
        configService.setConfig(Config.CACHE_SIZE, getPara("cacheSize"));
        configService.setConfig(Config.USE_PROXY, getPara("useProxy"));
        configService.setConfig(Config.PROXY_HOST, getPara("proxyHost"));
        configService.setConfig(Config.PROXY_PORT, getPara("proxyPort"));
        redirect("/config?saved=true");
    }
}
